package ihm;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JPasswordField;

import network.Connexion;
import network.FileSynchro;
//import network.SocketUtilisateur;

import data.Authentification;
import data.Log;
import data.Utilisateur;

public class FenetreAuthentifier extends JFrame  {

	private JPanel contentPane;
	private JTextField txtIp;
	private JTextField txtPort;

	/**
	 * Create the frame.
	 */
	public FenetreAuthentifier(final Connexion connexion, final Log log) {
		super();
		/*
		if(! con.getState()){
			System.err.println("Authentification imposible");
			return;
		}
		*/
		
		setTitle("Murder Party - Connexion");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 164);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblIdentifiant = new JLabel("Ip :");
		
		JLabel lblMotDePasse = new JLabel("Port :");
		
		txtIp = new JTextField();
		txtIp.setText("127.0.0.1");
		txtIp.setColumns(10);
		txtPort = new JTextField();
		
		// port par default
		lblMotDePasse.setVisible(false);
		txtPort.setVisible(false);
		
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JButton btnValider = new JButton("Valider");
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//authentifie = Utilisateur.sauthentifier(txtIdentifiant.getText(), strMotDePasse);
				
				log.addLine("[+] Connexion ...");
				// fenetre connexion
				connexion.connect(txtIp.getText() , 7000 );
				FileSynchro dropbox = new FileSynchro(connexion,"data/");
				dropbox.start();
				
				dispose();
			}
		});
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblIdentifiant)
						.addComponent(lblMotDePasse))
					.addGap(26)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(txtPort)
						.addComponent(txtIp, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
					.addContainerGap(63, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(212, Short.MAX_VALUE)
					.addComponent(btnValider)
					.addGap(18)
					.addComponent(btnAnnuler)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIdentifiant)
						.addComponent(txtIp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMotDePasse)
						.addComponent(txtPort, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAnnuler)
						.addComponent(btnValider)))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
